package crud;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import util.DBUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class Find {

	private static DBCollection collection = null;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		DB db = DBUtil.getDB("mydb");
		collection = db.getCollection("crud");
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testAnd() {
		List<BasicDBObject> criteria = new ArrayList<BasicDBObject>();
		criteria.add(new BasicDBObject("weight", new BasicDBObject("$ne", 101)));
		criteria.add(new BasicDBObject("weight", new BasicDBObject("$ne", 102)));
		
		DBCursor dbCursor = collection.find(new BasicDBObject("$and", criteria));
		
		//BasicDBObject criteria = new BasicDBObject("likes.color", "red").append("weight", 101);
		//DBCursor dbCursor = collection.find(criteria);
		System.out.println(dbCursor.size());
		while (dbCursor.hasNext()) {
			DBObject next = dbCursor.next();
			System.out.println(next.toMap());
		}
	}

}
